package com.buttonpay.home;

import java.util.ArrayList; 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup; 
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast; 
import com.buttonpay.AppData;
import com.buttonpay.DataHelper;
import com.buttonpay.Products;
import com.buttonpay.R;
import com.buttonpay.settings.SettingList;

public class HomeScreen extends Activity
{
	Button btnBack,btnLogOut;
	GridView menu;
	ArrayList<Products> productList=new ArrayList<Products>();
	ArrayList<Integer> servicesImage;
	ArrayList<String> servicesName;
	DataHelper datahelper;
	
	public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hometab);
       
		btnLogOut=(Button)findViewById(R.id.btnLogout); 
		btnBack=(Button)findViewById(R.id.btnPrevious);

		menu=(GridView)findViewById(R.id.menuView);
		btnBack.setVisibility(Button.INVISIBLE);
 
		setProductsInMenu();   
		   
		 btnLogOut.setOnClickListener(new OnClickListener()
		 {
			
			public void onClick(View v)
			{
				  new AlertDialog.Builder(HomeScreen.this)
				  	.setTitle( "Exit Application" )
			        .setMessage( "Are you sure you want to Logout?" )
			        .setPositiveButton("YES", new android.content.DialogInterface.OnClickListener() 
			        {
			            public void onClick(DialogInterface arg0, int arg1) 
			            {
			               finish();
			            }

						public void onClick(View v) 
						{
							// TODO Auto-generated method stub
							
						}
			        })
			        .setNegativeButton("NO",  new android.content.DialogInterface.OnClickListener() {
			            public void onClick(DialogInterface arg0, int arg1)
			            {
			                //do stuff onclick of CANCEL
			            	arg0.dismiss();
			               
			            }

						public void onClick(View v) 
						{
							// TODO Auto-generated method stub
						}
			        }).show();
		  	}
			
		 });
	}
	
	private void setProductsInMenu()
	{
		datahelper=new DataHelper(this);
		System.out.println("Retriving M-top up Products.");
		productList=datahelper.getProducts("2");
		datahelper.close();
		servicesName=new ArrayList<String>();
		servicesImage=new ArrayList<Integer>();
		
		if(productList!=null)
		{
			AppData.setTopUpProductList(productList);
			
			 for(Products p:productList)
	         { 
					 servicesName.add("Airtime TopUp");
					 servicesImage.add(R.drawable.top_up);
				 break;
	         }
		}
		else
		{
			 servicesName.add("No Products");
			 servicesImage.add(R.drawable.top_up);
		}
		 
		 servicesName.add("Utility Pay");
		 servicesName.add("My Account");
		 servicesName.add("Transaction History");
		 servicesName.add("Business Summary");
		 servicesName.add("Settings"); 
		 
		 servicesImage.add(R.drawable.bill_pay);
		 servicesImage.add(R.drawable.my_account);
		 servicesImage.add(R.drawable.transaction);
		 servicesImage.add(R.drawable.transaction);
		 servicesImage.add(R.drawable.help); 
		 menu.setAdapter(new ImageAdapter(this,servicesName,servicesImage));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		System.out.println("on activity result called =>resCode"+resultCode +",reqcode"+requestCode);
	    if(resultCode==2 && requestCode==11)
	    {
	        finish();
	    }
	    if(resultCode==1 && requestCode==11)
	    {
	        setProductsInMenu();
	    }
	}
	 
	 
	
	@Override
	public void onBackPressed() 
	{
		// TODO Auto-generated method stub
		
		new AlertDialog.Builder(HomeScreen.this)
		.setTitle( "Exit Application." )
        .setMessage( "Are you sure you want to Exit ?" )
        .setPositiveButton("YES", new android.content.DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface arg0, int arg1) 
            {
                //do stuff onclick of YES
            	finish();
            }
            
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				
			}
        })
        .setNegativeButton("NO",  new android.content.DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1)
            {
                //do stuff onclick of CANCEL
            	arg0.dismiss();
               
            }

			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
			}
        }).show();
	}
	public class ViewHolder
	{
			public TextView textView;
			public Button buttonView; 
	
    }
	public class ImageAdapter extends BaseAdapter 
	{
	    private Context mContext;
	     
	    ArrayList<String> services;
	    ArrayList<Integer> images;
 
	 
		public ImageAdapter(Context c,
				ArrayList<String> servicesName, ArrayList<Integer> servicesImage)
		{
			mContext = c;
			this.services=servicesName;
			this.images=servicesImage;
	    	 
		}
	 
		public int getCount() 
	    {
	        return services.size();
	    }

	    public Object getItem(int position) 
	    {
	        return null;
	    }

	    public long getItemId(int position) 
	    {
	        return position;
	    }
	    
	    // create a new ImageView for each item referenced by the Adapter
	    public View getView(final int position, View convertView, ViewGroup parent) 
	    { 
	    	View v=convertView;
	    	ViewHolder holder=null;
	    	if(v==null)
	    	{
	    		holder=new ViewHolder();
	    		 LayoutInflater li = getLayoutInflater();
	    		 v=li.inflate(R.layout.grid_item, null);
	    		 holder.buttonView=(Button)v.findViewById(R.id.grid_item_image);
	    		 holder.textView=(TextView)v.findViewById(R.id.grid_item_text);
	    		 v.setTag(holder);
	    	}
	    	else
	    		holder=(ViewHolder)v.getTag();
	    		holder.textView.setText(services.get(position));			
	    		holder.buttonView.setBackgroundResource(images.get(position));
	    		
	    		holder.buttonView.setOnClickListener(new OnClickListener() 
		        {
					public void onClick(View v) 
					{
						if(position==0)
						{
							if(productList!=null)
								startActivityForResult(new Intent(HomeScreen.this,Top_up.class), 11);
							else
								Toast.makeText(mContext, "No Products Available",Toast.LENGTH_SHORT).show();
						}
						else if(position==1)
						{
							startActivityForResult(new Intent(HomeScreen.this,Utilities.class),11);	
						}
						else if(position==2)
						{
							startActivityForResult(new Intent(HomeScreen.this,MyAccount.class), 11);
						}
						
						else if(position==3)
						{
							startActivityForResult(new Intent(HomeScreen.this,TransactionHistory.class),11);
						}
						else if(position==4)
						{
							new SummryDialog(HomeScreen.this).show();
							//startActivityForResult(new Intent(HomeScreen.this,ComingSoon.class), 11); 
						}
						else if(position==5)
						{
							startActivityForResult(new Intent(HomeScreen.this,SettingList.class), 11); 
						}
					}
				});
			return v; 
	    }
	    
	}
	 
	
}
